import json
import re
import requests
from fake_useragent import FakeUserAgent

# 前五条
ua = FakeUserAgent()

url = ('https://feed.sina.com.cn/api/roll/get?pageid=121&lid=1356&num=20&versionNumber=1.2.4&page=1&encode=utf-8'
       '&callback=feedCardJsonpCallback&_=1744205446026')
header = {
    'User-Agent': ua.random,
    'referer': 'https://news.sina.com.cn/china/'
}
web_data = requests.get(url, headers=header).text
# try{feedCardJsonpCallback( ... );} catch (e) {};
# 只要括号里面的内容
web_data = web_data.split('(')[1].split(')')[0]
web_data_json = json.loads(web_data)
for i in web_data_json['result']['data']:
    print(i['url'])
    articles = requests.get(i['url'], headers=header).content.decode()
    if re.findall(r'<div class="article" id="article">(.*?)</div>', articles, re.S):
        contents = re.findall(r'<div class="article" id="article">(.*?)</div>', articles, re.S)
        for content in contents:
            print("-" * 50)
            print(content)
    else:
        contents = re.findall(r'<p class="art_p">.*?</p><p class="art_p" cms-style="font-L">(.*?)</p>	', articles)
        for content in contents:
            print("-" * 50)
            print(content)
